package com.zivwu.library.ui.photo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zivwu.library.PhotoPicker;
import com.zivwu.library.R;
import com.zivwu.library.dao.bean.Photo;
import com.zivwu.library.ui.base.IFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ListPhotoFragment extends IFragment implements BaseQuickAdapter.OnItemChildClickListener {


    private static final int COUNT = 3;
    @Bind(R.id.rv_photo)
    RecyclerView rvPhoto;


    private int canSelectCount;
    private List<Photo> photos;
    private PhotoAdapter photoAdapter;
    private List<Photo> checkPhotos;
    private onSelected selectedListener;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof onSelected)
            selectedListener = (onSelected) context;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof onSelected)
            selectedListener = (onSelected) activity;
    }

    public static ListPhotoFragment newInstance(int canSelectCount, ArrayList<Photo> photos) {
        ListPhotoFragment fragment = new ListPhotoFragment();
        Bundle args = new Bundle();
        args.putInt(PhotoPicker.EXTRA_MAX_COUNT, canSelectCount);
        args.putParcelableArrayList(PhotoPicker.EXTRA_PHOTO_DATA, photos);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            canSelectCount = arguments.getInt(PhotoPicker.EXTRA_MAX_COUNT, canSelectCount);
            photos = arguments.getParcelableArrayList(PhotoPicker.EXTRA_PHOTO_DATA);
        }
        checkPhotos = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.ziv_fragment_list_photo, container, false);
        ButterKnife.bind(this, rootView);
        initListView();
        return rootView;
    }

    private void initListView() {
        photoAdapter = new PhotoAdapter(photos);
        photoAdapter.setOnItemChildClickListener(this);
        rvPhoto.setAdapter(photoAdapter);
        rvPhoto.setLayoutManager(new GridLayoutManager(getContext(), COUNT));
//        rvPhoto.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
    }


    public void setPhotoData(List<Photo> photos) {
        this.photos = photos;
        photoAdapter.setNewData(photos);
    }


    public List<Photo> getCheckPhotos() {
        return checkPhotos;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_checkbox:
                onCheckViewClicked(adapter, position);
                break;
            case R.id.iv_photo:
                PhotoPicker.builder()
                        .setPreviewPhoto((ArrayList<Photo>) photos)
                        .setPhotoIndex(position)
                        .start(getActivity());
                break;
        }


    }

    private void onCheckViewClicked(BaseQuickAdapter adapter, int position) {
        Photo photo = (Photo) adapter.getData().get(position);
        if (photo.isChecked()) {
            //item 已经被选中
            photo.setChecked(false);
            checkPhotos.remove(photo);
        } else {
            singleSelected(adapter);

            if (checkPhotos.size() >= canSelectCount && canSelectCount!=1) {
                //超过可以选择的最大数量
                showToast(getString(R.string.ziv_out_of_photo_can_select, canSelectCount));
                return;
            }
            photo.setChecked(true);
            checkPhotos.add(photo);
        }
        adapter.notifyItemChanged(position);
        if (selectedListener!=null)
            selectedListener.onSelected(checkPhotos.size());
    }

    private void singleSelected(BaseQuickAdapter adapter) {
        if (canSelectCount != 1)
            return ;
        if (checkPhotos.size() == 1) {
            Photo photo1 = checkPhotos.get(0);
            photo1.setChecked(false);
            checkPhotos.remove(photo1);
            int indexOf = photos.indexOf(photo1);
            adapter.notifyItemChanged(indexOf);
        }

    }


    interface onSelected {
        void onSelected(int size);
    }
}
